package gameserver.utils;

import gameserver.configs.main.CustomConfig;
import gameserver.model.gameobjects.player.Player;

public class PacketSendUtility
{
	public static void sendMessage(Player player,String msg)
	{
		String[] msgPiece = Util.splitStringFixedLen(msg, CustomConfig.MESSAGE_LENGTH);
		for(int i = 0; i < msgPiece.length; ++i)
		{
			sendPacket(player, new SM_MESSAGE(0, null, msgPiece[i], ChatType.ANNOUNCEMENTS));
		}
	}
	
	public static void sendSysMessage(Player player, String msg) {
        String[] msgPiece = Util.splitStringFixedLen(msg, CustomConfig.MESSAGE_LENGTH);
        for (int i = 0; i < msgPiece.length; ++i) {
            sendPacket(player, new SM_MESSAGE(0, null, msgPiece[i], ChatType.SYSTEM_NOTICE));
        }
    }

    /**
     * Send packet to this player.
     *
     * @param player
     * @param packet
     */
    public static void sendPacket(Player player, AionServerPacket packet) {
        if (player != null && player.getClientConnection() != null)
            player.getClientConnection().sendPacket(packet);
    }
    
    public static void sendPacketSeq(Player player, AionServerPacketSeq packetSeq) {
        if (player.getClientConnection() != null)
            player.getClientConnection().sendPacketSeq(packetSeq);
     }

    /**
     * Broadcast packet to all visible players.
     *
     * @param player
     * @param packet ServerPacket that will be broadcast
     * @param toSelf true if packet should also be sent to this player
     */
    public static void broadcastPacket(Player player, AionServerPacket packet, boolean toSelf) {
        if (toSelf)
            sendPacket(player, packet);

        broadcastPacket(player, packet);
    }

    /**
     * Broadcast packet to all visible players.
     *
     * @param visibleObject
     * @param packet
     */
    public static void broadcastPacketAndReceive(VisibleObject visibleObject, AionServerPacket packet) {
        if (visibleObject instanceof Player)
            sendPacket((Player) visibleObject, packet);

        broadcastPacket(visibleObject, packet);
    }

    /**
     * Broadcast packet to all Players from knownList of the given visible object.
     *
     * @param visibleObject
     * @param packet
     */
    public static void broadcastPacket(VisibleObject visibleObject, final AionServerPacket packet) {
        visibleObject.getKnownList().doOnAllPlayers(new Executor<Player>() {
            @Override
            public boolean run(Player obj) {
                sendPacket(obj, packet);
                return true;
            }
        }, true);
    }

	/**
	 * Broadcasts packet to all Players from knownList of the given visible object within the specified distance in meters
	 * 
	 * @param visibleObject
	 * @param packet
	 * @param distance
	 */
	public static void broadcastPacket(final VisibleObject visibleObject, final AionServerPacket packet, final int distance){
		visibleObject.getKnownList().doOnAllPlayers(new Executor<Player>(){
			@Override
			public boolean run(Player p){
				if(MathUtil.getDistance(visibleObject, p) <= distance){
					sendPacket(p, packet);
				}
				return true;
			}
		}, true);
	}
	
    /**
     * Broadcasts packet to all visible players matching a filter
     *
     * @param player
     * @param packet ServerPacket to be broadcast
     * @param toSelf true if packet should also be sent to this player
     * @param filter filter determining who should be messaged
     */
    public static void broadcastPacket(Player player, final AionServerPacket packet, boolean toSelf, final ObjectFilter<Player> filter) {
        if (toSelf) {
            sendPacket(player, packet);
        }

        player.getKnownList().doOnAllPlayers(new Executor<Player>() {
            @Override
            public boolean run(Player target) {
                if (filter.acceptObject(target))
                    sendPacket(target, packet);
                return true;
            }
        }, true);
    }
}
